package com.demo.thread.demo_thread_create_way;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author peter
 * @date 2022/3/10 8:59
 */
public class Demo3_1 implements Callable<String> {

    @Override
    public String call() throws Exception {
        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName()+"-当前剩余票数:"+i);
        }
        return "Sale out";
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        Demo3_1 callable = new Demo3_1();

        // futureTask
        FutureTask<String> futureTask = new FutureTask<String>(callable);
        // futureTask只会执行一次
        Thread thread1 = new Thread(futureTask, "线程1");
        thread1.start();
        // futureTask 有返回值
        String result = futureTask.get();
        System.out.println(result);
    }
}
